/*
带有跟随功能组件
 */
const { ccclass, property, requireComponent } = cc._decorator;

@ccclass
@requireComponent(cc.Camera)
export default class LookAtTargetCamera extends cc.Component {

    @property
    _target: cc.Node = null
    @property({ type: cc.Node, tooltip: "跟随目标" })
    get target(): cc.Node { return this._target }
    set target(value) { this._target = value }

    @property
    targetPath: string = "Canvas/Map/Player/main"

    private _camera: cc.Camera
    // LIFE-CYCLE CALLBACKS:

    onLoad() { }
    start() {
        this._camera = this.node.getComponent(cc.Camera)
    }
    update(dt: number) {
        if (this.target) {
            let worldPos = this.target.getParent().convertToWorldSpaceAR(this.target.getPosition())
            let localPos = this.node.getParent().convertToNodeSpaceAR(worldPos)
            this.node.setPosition(localPos)
        } else {
            this._target = cc.find(this.targetPath)
        }
    }
}
